package lunchy.mvc.views;

import java.util.List;

import org.eclipse.swt.widgets.Shell;

import lunchy.main.CLunchyApplication;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;

import lunchy.data.interfaces.ILunchyMenuGroup;
import lunchy.data.interfaces.ILunchyMenuItem;
import lunchy.mvc.controllers.CLunchyMenuEditGroupController;
import lunchy.mvc.controllers.CLunchyMenuEditItemController;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Spinner;

public class CLunchyMenuEditItemView
{
	private Shell shellMenuEditItem;
	private CLunchyMenuEditItemController controller;
	private Text itemName;
	private Spinner itemPrice;

	public CLunchyMenuEditItemView(ILunchyMenuItem item)
	{
		controller = new CLunchyMenuEditItemController(item);
		
		shellMenuEditItem = new Shell(CLunchyApplication.getInstance().display, SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.APPLICATION_MODAL);
		shellMenuEditItem.setMinimumSize(new Point(200, 80));
		shellMenuEditItem.setText("LunchY | Menu Edit");
		shellMenuEditItem.setSize(272, 115);

		initUI();
		
		itemName.setText(item.getName());
		itemPrice.setSelection(item.getPrice());
		
		shellMenuEditItem.open();
        
        while (!shellMenuEditItem.isDisposed())
        {
        	if (!CLunchyApplication.getInstance().display.readAndDispatch())
        		CLunchyApplication.getInstance().display.sleep();
        }
	}

	private void initUI()
	{
		Label nameLabel = new Label(shellMenuEditItem, SWT.NONE);
		nameLabel.setBounds(10, 10, 59, 14);
		nameLabel.setText("Название");
		
		itemName = new Text(shellMenuEditItem, SWT.BORDER);
		itemName.setBounds(75, 7, 187, 19);

		Label priceLabel = new Label(shellMenuEditItem, SWT.NONE);
		priceLabel.setBounds(10, 37, 59, 14);
		priceLabel.setText("Цена");
		
		itemPrice = new Spinner(shellMenuEditItem, SWT.BORDER);
		itemPrice.setBounds(75, 32, 187, 22);
		itemPrice.setMinimum(0);
		itemPrice.setMaximum(99999);
		
		Button addButton = new Button(shellMenuEditItem, SWT.NONE);
		addButton.setBounds(75, 60, 94, 28);
		addButton.setText("Сохранить");
		addButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					controller.modifyItem(itemName.getText(), itemPrice.getText());
					shellMenuEditItem.dispose();
				}

				public void mouseDown(MouseEvent e)
				{
	
				}

				public void mouseDoubleClick(MouseEvent e)
				{

				}
			}
		);
		
		Button cancelButton = new Button(shellMenuEditItem, SWT.NONE);
		cancelButton.setBounds(168, 60, 94, 28);
		cancelButton.setText("Отмена");
		cancelButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					shellMenuEditItem.dispose();
				}

				public void mouseDown(MouseEvent e)
				{

				}

				public void mouseDoubleClick(MouseEvent e)
				{

				}
			}
		);
	}
}
